# -*- coding: UTF-8 -*-    
# Author:yangshuoshuo
# FileName:re模块
# DateTime:2024/7/14 17:38
# SoftWare: PyCharm
import re

print(re.match("\w+", "嘿嘿嘿"))

print(re.search("\d+", "2344fff555"))
print(re.search("\d+", "2344fff555555").group())
print(re.search("\d+", "2344fff555").span())


print(re.findall("\d+", "aa11123lkjhjh7897sdj988"))

print("aaa\nbbb")
print(r"aaa\nbbb")
print("aaa\\nbbb")

pattern = r"\d+"
string = "12345hello world"
match = re.match(pattern, string)
print(match.group())


pattern = r"hello"
string = "hello world"
match = re.match(pattern, string)
print(match.span())

print(match.end())

import re

pattern = r"\d+"  # 匹配一个或多个数字
string = "abc 123 def 456 ghi"

# 将字符串中的数字替换为 "NUM"
result = re.sub(pattern, "NUM", string)
print("替换后的字符串:", result)


import re

pattern = r'\s+'  # 匹配一个或多个空白字符
string = 'Hello   World  Python'

# 使用正则表达式模式分割字符串
result = re.split(pattern, string)
print("分割后的子字符串:", result)

# 结果：
# 分割后的子字符串: ['Hello', 'World', 'Python']


import re

pattern = r'^\d+'  # 匹配每行开头的数字
string = '123 apple\n456 banana\n789 cherry'

matches = re.findall(pattern, string)
print("单行匹配的结果:", matches)

# 使用 re.MULTILINE 标志进行匹配
matches = re.findall(pattern, string, re.MULTILINE)
print("多行匹配的结果:", matches)
